﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Globalization;
using System.Web.Security;

namespace VoteSystem.Models
{
    public class VoteSystemContext : DbContext
    {
        public VoteSystemContext ()
            : base("DefaultConnection")
        {
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
        }
        /// <summary>
        /// 能够操作系统的帐号
        /// </summary>
        public DbSet<UserInfo> UserProfiles { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        public DbSet<Role> Roles { get; set; }
        
        /// <summary>
        /// 岗位
        /// </summary>
        public DbSet<Position> Positions { get; set; }

        /// <summary>
        /// 竞聘人员
        /// </summary>
        public DbSet<Employee> Employees { get; set; }

        /// <summary>
        /// 竞聘配置
        /// </summary>
        public DbSet<VoteConfig> VoteConfigs { get; set; }

        /// <summary>
        /// 员工竞聘职位记录
        /// </summary>
        public DbSet<EmployeeVotePosition> EmployeeVotePositions { get; set; }

        public DbSet<SysConfig> SysConfigs { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        public DbSet<VoteResult> VoteResults { get; set; }
    }


    [Table("VoteResults")]
    public class VoteResult
    { 

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        [ForeignKey("Config")]
        public int VoteConfigId { get; set; }

        public virtual VoteConfig Config { get; set; }

        [Required]
        [ForeignKey("Position")]
        public int PositionId { get; set; }

        public virtual Position Position { get; set; }

        [Required]
        [ForeignKey("Employee")]
        public int EmployeeId { get; set; }

        public virtual Employee Employee { get; set; }

        /// <summary>
        /// 第几自愿定聘的
        /// </summary>
        public int Round { get; set; }

    }

    [Table("Roles")]
    public class Role 
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int RoleId { get; set; }
        [Required]
        [StringLength(50)]
        public string RoleName { get; set; }
    }

    [Table("SysConfigs")]
    public class SysConfig 
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 是否已导入岗位
        /// </summary>
        [Required]
        public bool ImportPosition { get; set; }


    }

    [Table("UserInfo")]
    public class UserInfo
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        [StringLength(50)]
        [Required]
        public string UserName { get; set; }
        [StringLength(50)]
        public string DisplayName { get; set; }
        [StringLength(128)]
        [Required]
        public string Password { get; set; }
        /// <summary>
        /// 上次登录IP
        /// </summary>
        [StringLength(20)]
        public string LastLoginIPAdress { get; set; }
        /// <summary>
        /// 上次登录日期
        /// </summary>
        public DateTime? LastLoginDate { get; set; }
        /// <summary>
        /// 是否被锁定
        /// </summary>
        public bool IsLock { get; set; }

        [ForeignKey("Role")]
        [Required]
        public int RoleId { get; set; }
        public virtual Role Role { get; set; }

        /// <summary>
        /// 登录操作员属于哪个科室
        /// </summary>
        [Required]
        public string DeptName { get; set; }

        public UserInfo()
        {
            IsLock = false;
        }
    }

    /// <summary>
    /// 配置
    /// </summary>
    public class VoteConfig
    { 
    
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        /// <summary>
        /// 第几轮定聘
        /// </summary>
        [Required]
        public int Count { get; set; }

        /// <summary>
        /// 本轮第1志愿定聘开始时间
        /// </summary>
        [Required] 
        public DateTime StartTime1{ get; set; }

        /// <summary>
        /// 本轮第1志愿定聘结束时间
        /// </summary>
        [Required] 
        public DateTime EndTime1{ get; set; }

        /// <summary>
        /// 本轮第2志愿定聘开始时间
        /// </summary>
        [Required] 
        public DateTime StartTime2{ get; set; }

        /// <summary>
        /// 本轮第2志愿定聘结束时间
        /// </summary>
        [Required] 
        public DateTime EndTime2{ get; set; }

        /// <summary>
        /// 本轮第3志愿定聘开始时间
        /// </summary>
        [Required] 
        public DateTime StartTime3{ get; set; }

        /// <summary>
        /// 本轮第3志愿定聘结束时间
        /// </summary>
        [Required] 
        public DateTime EndTime3{ get; set; }

        /// <summary>
        /// 每轮竞聘只有一个配置生效
        /// </summary>
        [Required]
        public bool Active { get; set; }
    }

    public class Employee
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int EmployeeId { get; set; }

        [StringLength(50)]
        public string Name { get; set; }

        [StringLength(20)]
        public string Age { get; set; }
        /// <summary>
        /// 最高学历
        /// </summary>
        [StringLength(50)]
        public string Education { get; set; }
        /// <summary>
        /// 主修
        /// </summary>
        [StringLength(50)]
        public string Major { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        [StringLength(50)]
        public string Mobile { get; set; }
       
        [StringLength(20)]
        public string Sex { get; set; }
    }

    public class Position
    {

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        /// <summary>
        /// 科室
        /// </summary>
        public string DeptName { get; set; }

        /// <summary>
        /// 岗位
        /// </summary>
        public string PositionName { get; set; }
    }


    public class EmployeeVotePosition
    { 
    
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [ForeignKey("Employee")]
        [Required]
        public int EmployeeId { get; set; }

        public virtual Employee Employee { get; set; }

        [ForeignKey("Config")]
        [Required]
        public int ConfigId { get; set; }

        public virtual VoteConfig Config { get; set; }

       /// <summary>
       /// 第1志愿
       /// </summary>
        [ForeignKey("Position1")]
        [Required]
        public int PositionId1 { get; set; }

        public virtual Position Position1 { get; set; }


       /// <summary>
       /// 第2志愿
       /// </summary>
        [ForeignKey("Position2")]
        [Required]
        public int PositionId2 { get; set; }

        public virtual Position Position2 { get; set; }
       /// <summary>
       /// 第3志愿
       /// </summary>
        [ForeignKey("Position3")]
        [Required]
        public int PositionId3 { get; set; }

        public virtual Position Position3 { get; set; }
    }

    public class RegisterExternalLoginModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        public string ExternalLoginData { get; set; }
    }

    public class LocalPasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "当前密码")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "新密码")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认新密码")]
        [Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

    public class LoginModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [Display(Name = "记住我?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

    public class ExternalLogin
    {
        public string Provider { get; set; }
        public string ProviderDisplayName { get; set; }
        public string ProviderUserId { get; set; }
    }

    public class VoteViewModel
    {
        public string DeptName { get; set; }

        public string PositionName { get; set; }

        public int PositionId { get; set; }

        /// <summary>
        /// 该岗位第1志愿填报的职员列表
        /// </summary>
        public List<VoteItemViewModel> EmployeeVote1 { get; set; }     

        /// <summary>
        /// 该岗位第2志愿填报的职员列表
        /// </summary>
        public List<VoteItemViewModel> EmployeeVote2 { get; set; }     
        /// <summary>
        /// 该岗位第3志愿填报的职员列表
        /// </summary>
        public List<VoteItemViewModel> EmployeeVote3 { get; set; }     

        public VoteViewModel()
        {
            EmployeeVote1 = new List<VoteItemViewModel>();
            EmployeeVote2 = new List<VoteItemViewModel>();
            EmployeeVote3 = new List<VoteItemViewModel>();
        }
    }

    public class VoteItemViewModel
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
    }

}
